/* GUI CONVERTED from rssupc.p (converter v1.75) Sat Aug 12 23:16:55 2000 */
/* rssupc.p - Routine to generate Planning Schedules                        */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.             */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=No                                                          */
/* REVISION: 9.0          CREATED: 06/25/98     BY: *K1QZ* Suresh Nayak     */
/* REVISION: 9.0    LAST MODIFIED: 03/13/99     BY: *M0BD* Alfred Tan       */
/* REVISION: 9.1    LAST MODIFIED: 08/12/00     BY: *N0KP* myb              */




/* -----  W A R N I N G  -----  W A R N I N G  -----  W A R N I N G  -----  */
/*                                                                          */
/*         THIS PROGRAM IS A BOLT-ON TO STANDARD PRODUCT MFG/PRO.           */
/* ANY CHANGES TO THIS PROGRAM MUST BE PLACED ON A SEPARATE ECO THAN        */
/* STANDARD PRODUCT CHANGES.  FAILURE TO DO THIS CAUSES THE BOLT-ON CODE TO */
/* BE COMBINED WITH STANDARD PRODUCT AND RESULTS IN PATCH DELIVERY FAILURES.*/
/*                                                                          */
/* -----  W A R N I N G  -----  W A R N I N G  -----  W A R N I N G  -----  */



         /* DISPLAY TITLE */

/*GUI preprocessor directive settings */
&SCOPED-DEFINE PP_GUI_CONVERT_MODE REPORT

         {mfdeclre.i}

/* **************** Begin Translatable Strings Definitions **************** */


/* ***************** End Translatable Strings Definitions ***************** */


            define input parameter pod_recno as recid no-undo.
            define input parameter sch_recno as recid no-undo.
            define input parameter report_yn as logical no-undo.
            define input parameter asof_date as date no-undo.

            define variable interval as character no-undo.
            define variable work_dt as date no-undo.
            define variable l_pod_plan_days like pod_plan_days no-undo.
            define variable lastdaysdt as date no-undo.
            define variable lastweeksdt as date no-undo.
            define variable lastmthsdt as date no-undo.
            define variable last_firm_date as date no-undo.
            define variable before as logical initial yes no-undo.
            define variable idate as date no-undo.
            define variable last_date as date no-undo.
            define variable plan_schedule as integer no-undo.
            define variable new_cum_qty as decimal no-undo.
            define shared variable date_based_rel_id like mfc_logical no-undo.
            define variable l_rlse_id like sch_rlse_id no-undo.
            define variable curr_rel_id as integer no-undo.
            define variable create_zero_sched like mfc_logical no-undo.
            define variable zero_schedule like mfc_logical no-undo.
            define variable zero_schedule_created like mfc_logical no-undo.

            define buffer type_5_sch_mstr for sch_mstr.
            define workfile work_schd like schd_det.

            for first pod_det where recid(pod_det) = pod_recno
            exclusive-lock: end.

            for first sch_mstr where recid(sch_mstr) = sch_recno
            no-lock: end.

            /* DELETING THE WORK FILE */
            for each work_schd exclusive-lock:
                delete work_schd.
            end.

            assign plan_schedule = 5.

            /* GETTING NEXT RELEASE NUMBER FOR PLANNING SCHEDULE */

            if pod__qad11 = ? or pod__qad11    = "" then
               pod__qad11 = string(1,"9999999").

            /* DETERMINING IF DATE BASED RELEASE ID'S NEEDS TO BE GENERATED */
            if date_based_rel_id then do:

                curr_rel_id = 0.

                l_rlse_id = string(year(asof_date),"9999") +
                            string(month(asof_date),"99")  +
                            string(day(asof_date),"99")    + "-".

                for last type_5_sch_mstr where
                    type_5_sch_mstr.sch_type = 5 and
                    type_5_sch_mstr.sch_nbr = pod_nbr and
                    type_5_sch_mstr.sch_line = pod_line and
                    type_5_sch_mstr.sch_rlse_id begins l_rlse_id exclusive-lock:
                 end.

                 if not available type_5_sch_mstr then do:
                    l_rlse_id = l_rlse_id + string(001,"999").
                 end.
                 else do:
                   curr_rel_id =
                        integer(substring(type_5_sch_mstr.sch_rlse_id,11,3)).

                   if curr_rel_id + 1 > 999 then do:
                     l_rlse_id = l_rlse_id + string(001,"999").
                   end.
                   else do:
                     l_rlse_id = l_rlse_id +
                     string(integer(substring(type_5_sch_mstr.sch_rlse_id,11,3))
                                  + 1,"999").
                   end.

                    /* SET END EFFECTIVE DATE ON PREVIOUS SCHEDULE */
                       assign
                         type_5_sch_mstr.sch_eff_end = asof_date
                         .
                 end. /* else do: */
            end. /* END OF IF date_based_rel_id */

            else do:
                repeat while can-find(type_5_sch_mstr
                where type_5_sch_mstr.sch_type = plan_schedule and
                      type_5_sch_mstr.sch_nbr  = pod_nbr       and
                      type_5_sch_mstr.sch_line = pod_line      and
                      type_5_sch_mstr.sch_rlse_id = pod__qad11):

                    pod__qad11 = string((integer(pod__qad11) + 1),"999999").
                    if integer(pod__qad11) = 999999 then
                       pod__qad11 = string(1,"999999").

                end. /* END OF REPEAT WHILE CAN-FIND(sch_mstr) */
            end. /* END OF ELSE DO */

            create type_5_sch_mstr.

            assign
                type_5_sch_mstr.sch_type = plan_schedule
                type_5_sch_mstr.sch_nbr = pod_nbr
                type_5_sch_mstr.sch_line = pod_line
                type_5_sch_mstr.sch_rlse_id = (if date_based_rel_id then
                                               l_rlse_id else pod__qad11)
                type_5_sch_mstr.sch_cr_date = today
                type_5_sch_mstr.sch_cr_time = time
                type_5_sch_mstr.sch_eff_start = sch_mstr.sch_eff_start
                type_5_sch_mstr.sch_cumulative = sch_mstr.sch_cumulative
                type_5_sch_mstr.sch_fab_qty = sch_mstr.sch_fab_qty
                type_5_sch_mstr.sch_fab_end = sch_mstr.sch_fab_end
                type_5_sch_mstr.sch_fab_strt = sch_mstr.sch_fab_strt
                type_5_sch_mstr.sch_pcr_qty = sch_mstr.sch_pcr_qty
                type_5_sch_mstr.sch_pcs_date = sch_mstr.sch_pcs_date
                type_5_sch_mstr.sch_raw_qty = sch_mstr.sch_raw_qty
                type_5_sch_mstr.sch_raw_strt = sch_mstr.sch_raw_strt
                type_5_sch_mstr.sch_raw_end = sch_mstr.sch_raw_end
                type_5_sch_mstr.sch_sd_pat = sch_mstr.sch_sd_pat
                type_5_sch_mstr.sch_sd_time = sch_mstr.sch_sd_time
            .

            if recid(type_5_sch_mstr) = -1 then .

            assign
                pod_curr_rlse_id[plan_schedule - 3] =
                    type_5_sch_mstr.sch_rlse_id.


             /* IF SHIPPING SCHEDULES ARE NOTGENERATED THEN DAILY   */
             /* REQIUREMENTS WILL BE SHOWN ON THE PLANNING SCHEDULE */
             if pod__qad05 then
                 l_pod_plan_days = 0.
             else
                 l_pod_plan_days = pod_plan_days.

             /* DETERMINE THE ENDING DATE FOR THE WEEKLY BUCKETS AND */
             /* THE ENDING DATES FOR THE MONTHLY BUCKET.             */
             {gprun.i ""rscalcld.p""
                      "(input asof_date,
                        input l_pod_plan_days,
                        input pod_plan_weeks,
                        input pod_plan_mths,
                        output lastdaysdt,
                        output lastweeksdt,
                        output lastmthsdt)"}

             /* FIND LAST FIRM  DATE  IN  SCHEDULE  TO  FIGURE  SCHEDULE  DAYS
                 BECAUSE FIRM QUANTITIES WILL NOT BE BUCKETED. CONCEPT OF
                 FIRM WILL BE USED IF ONLY THE PLANNING SCHEDULE IS GENERATED */

             last_firm_date = sch_mstr.sch_pcs_date + pod_firm_days.

             find last schd_det
             where schd_type = sch_mstr.sch_type
             and schd_nbr = sch_mstr.sch_nbr
             and schd_line = sch_mstr.sch_line
             and schd_rlse_id = sch_mstr.sch_rlse_id
             and schd_fc_qual = "f"
             no-lock no-error.

                 if available schd_det then
                     last_firm_date = max(last_firm_date, schd_date).

             for each schd_det exclusive-lock where
                 schd_type = sch_mstr.sch_type and
                 schd_nbr  = sch_mstr.sch_nbr and
                 schd_line = sch_mstr.sch_line and
                 schd_rlse_id = sch_mstr.sch_rlse_id
             break by schd_date:

                 assign
                     work_dt = ?
                     interval = "".

                 /* IF DAILY REQUIREMENTS ARE NOT SENT THEN WE NEED TO  */
                 /* SHOW DAILY REQUIREMENTS ON THE PLANNING SCHEDULE    */
                 if schd_date <= lastdaysdt then do:
                     work_dt = schd_date.
                     interval = "D".
                 end.
                 else
                 if schd_date <= lastweeksdt then do:

                     /* GET THE MONDAY OF THE WEEK THIS DATE IS IN */
                     {gprun.i ""rswmd.p""
                               "(input schd_date,
                                 input before,
                                 output work_dt)"
                     }

                     interval = "W".

                 end. /* END OF IF schd_date <= lastweeksdt */
                 else
                 if schd_date <= lastmthsdt then do:

                     /* GET THE MONDAY OF THE MONTH THIS DATE IS IN.   */
                     /* NOTE THAT IF THE DATE FALLS BEFORE THE FIRST   */
                     /* MONDAY OF THE MONTH OF THE DATE, WE USE THE    */
                     /* DATE OF THE FIRST MONDAY OF THE PRIOR MONTH    */

                     {gprun.i ""rsm1md.p""
                               "(input schd_date,
                                 input before,
                                 output work_dt)"
                     }

                     interval = "M" .

                 end. /* IF schd_date <= lastmthsdt */

                 if work_dt <> ? then do for work_schd:

                     create work_schd.

                     assign
                         schd_date = work_dt
                         schd_interval = interval
                         schd_discr_qty = schd_det.schd_discr_qty
                         schd_fc_qual = schd_det.schd_fc_qual.

                     if interval <> "D" then do:

                         assign
                             schd_time = ""
                             schd_reference = "".

                     end. /* IF interval <> "D"  */

                 end. /* IF work_dt <> ? THEN DO FOR work_schd  */

             end. /* END FOR EACH schd_det */

             run find_prev_plan_sched (input recid(sch_mstr),
                                       input recid(pod_det),
                                       output zero_schedule,
                                       output zero_schedule_created).

             /* WE ARE GOING TO ONLY MAKE A ONE LINE ZERO SCHEDULE IF
                ZERO SCHEDULES ARE TO BE GENERATED. OTHERWISE WE
                WILL NOT PRINT A SCHEDULE IF THERE ARE NO REQUIREMENTS */

             for first rsc_ctrl no-lock:
             end.
             if not available rsc_ctrl then create rsc_ctrl.

             for first po_mstr where po_nbr = pod_nbr no-lock:
             end.

             for first vd_mstr where vd_addr = po_vend no-lock:
             end.

             for first qad_wkfl where qad_key1 = "vd_mstr_ash" and
                                      qad_key2 = vd_addr no-lock:
             end.

             if available qad_wkfl then do:
                if substring(qad_charfld[4],1,3) = "yes" then do:
                   create_zero_sched = yes.
                end.
                else do:
                  create_zero_sched = no.
                end.
             end.
             else do:
                  create_zero_sched = rsc__qadl01.
             end.


             /* ENSURE THAT THERE ARE ATLEAST ZERO QUANTITIES FOR ALL */
             /* POSSIBLE BUCKET DATES                                 */

             last_date = lastmthsdt.
             if last_date = ? then last_date = lastweeksdt.
             if last_date = ? then last_date = lastdaysdt.

             idate = type_5_sch_mstr.sch_pcs_date + 1.

             do while idate <= last_date:

                 work_dt = ?.
                 if idate <= lastdaysdt then do:
                    work_dt = idate.
                    interval = "D".
                    idate = idate + 1.
                 end.
                 else
                 if idate <= lastweeksdt then do:

                     /* GET MONDAY OF THE WEEK THIS DATE IS IN */
                     {gprun.i ""rswmd.p""
                               "(input idate,
                                 input before,
                                 output work_dt)"
                     }

                     assign
                         interval = "W"
                         idate = work_dt + 7.

                 end. /* END OF IF idate <= lastweeksdt */
                 else if idate <= lastmthsdt then do:

                    /* GET MONDAY OF THE MONTH THIS DATE IS IN. NOTE THAT */
                    /* IF THIS DATE FALLS BEFORE THE FIRST MONDAY OF THE  */
                    /* OF THE MONTH OF THE DATE, WE USE THE DATE OF THE   */
                    /* FIRST MONDAY OF THE PRIOR MONTH                    */

                    {gprun.i ""rsm1md.p""
                             "(input idate,
                               input before,
                               output work_dt)"
                     }

                     assign
                         interval = "M"
                         idate = work_dt + 45.

                 end.  /* END OF IF idate <= lastmthsdt */

                 if work_dt <> ? then do for work_schd:

                     create work_schd.

                     assign
                         schd_date = work_dt
                         schd_interval = interval.

                     /* FIRM REQUIREMENTS WILL EXIST ONLY IF NO DAILY SHIPPING
                        SCHEDULES ARE GENERATED. IF DAILY SHIPPING SCHEDULES
                        ARE GENERATED THEN PLANNING SCHEDULES WILL NOT HAVE
                        FIRM REQUIREMENTS */

                     if schd_date <= last_firm_date and not(pod__qad05) then
                         schd_fc_qual = "F".
                     else
                         schd_fc_qual = "P".


                 end. /* END OF IF work_dt <> ? */

             end. /*  END OF DO WHILE idate <= last_date */


             /* CONSOLIDATE THE WORK FILE RECORDS AND CREATE SCHEDULE */
             /* DETAIL RECORDS                                        */

             for each work_schd no-lock
             break by schd_date
                   by schd_time
                   by schd_reference:

                   if create_zero_sched and
                      zero_schedule     and
                      zero_schedule_created then next.

                   if zero_schedule            and
                      not create_zero_sched    and
                      not zero_schedule_created then next.

                   accumulate work_schd.schd_discr_qty
                   (sub-total by work_schd.schd_reference).

                   if last-of(work_schd.schd_reference) then do:

                       create schd_det.

                       assign
                           schd_det.schd_type = type_5_sch_mstr.sch_type
                           schd_det.schd_nbr = type_5_sch_mstr.sch_nbr
                           schd_det.schd_line = type_5_sch_mstr.sch_line
                           schd_det.schd_rlse_id =
                               type_5_sch_mstr.sch_rlse_id
                           schd_det.schd_date = work_schd.schd_date
                           schd_det.schd_time = work_schd.schd_time
                           schd_det.schd_interval = work_schd.schd_interval
                           schd_det.schd_reference =
                               work_schd.schd_reference
                           schd_det.schd_discr_qty = accum sub-total by
                           schd_reference work_schd.schd_discr_qty
                           schd_det.schd_fc_qual = work_schd.schd_fc_qual.

                     if recid(schd_det) = -1 then .

                     if zero_schedule then do:
                        zero_schedule_created = yes.
                     end.

                   end. /* END OF IF last-of(schd_reference)  */

             end. /* FOR EACH work_schd */


             /* UPDATE CUM QUANTITIES */

             new_cum_qty = type_5_sch_mstr.sch_pcr_qty.

             for each schd_det exclusive-lock
             where schd_type = type_5_sch_mstr.sch_type
             and schd_nbr = type_5_sch_mstr.sch_nbr
             and schd_line = type_5_sch_mstr.sch_line
             and schd_rlse_id = type_5_sch_mstr.sch_rlse_id:
                new_cum_qty = new_cum_qty + schd_det.schd_discr_qty.
                schd_det.schd_cum_qty = new_cum_qty.
             end.


             /* REPORT THE PLANNING SCHEDULE */
             if report_yn then do:

                 {gprun.i
                      ""rsiq1a.p""
                       "(input type_5_sch_mstr.sch_type,
                         input type_5_sch_mstr.sch_nbr,
                         input type_5_sch_mstr.sch_line,
                         input type_5_sch_mstr.sch_rlse_id,
                         input no,
                         input no)"
                 }

             end. /* END OF IF report_yn */



             procedure find_prev_plan_sched:

                define input parameter sch_recno as recid no-undo.
                define input parameter pod_recno as recid no-undo.
                define output parameter zero_schedule like mfc_logical no-undo.
                define output parameter zero_schedule_created like mfc_logical
                       no-undo.

                zero_schedule = no.
                zero_schedule_created = no.

                for first sch_mstr where recid(sch_mstr) = sch_recno
                no-lock:
                end.

                for first pod_det where recid(pod_det) = pod_recno
                no-lock:
                end.

                for first schd_det where
                          schd_type = sch_type       and
                          schd_nbr  = sch_nbr        and
                          schd_line = sch_line       and
                          schd_rlse_id = sch_rlse_id
                 no-lock: end.

                if available schd_det then do:
                   find first schd_det where
                                 schd_type = sch_type        and
                                 schd_nbr  = sch_nbr         and
                                 schd_line = sch_line        and
                                 schd_rlse_id = sch_rlse_id  and
                                 schd_discr_qty > 0 no-lock no-error.
                     if available schd_det then do:
                          zero_schedule = no.
                     end.
                end. /* if available schd_det */
                else do:
                     zero_schedule = yes.
                end.

                find prev sch_mstr where
                          sch_type = 4        and
                          sch_nbr =  pod_nbr  and
                          sch_line = pod_line
                          use-index sch_cr_date no-lock no-error.

                if available sch_mstr then do:
                   find first schd_det where schd_type = 5 and
                        schd_nbr = sch_nbr         and
                        schd_line = sch_line       and
                        schd_rlse_id = sch_rlse_id
                        no-lock no-error.

                   if not available schd_det then do:
                      zero_schedule_created = no.
                   end.
                   else do:
                        find first schd_det where
                             schd_type = 5                       and
                             schd_nbr = sch_mstr.sch_nbr         and
                             schd_line = sch_mstr.sch_line       and
                             schd_rlse_id = sch_mstr.sch_rlse_id and
                             schd_discr_qty > 0
                             no-lock no-error.
                         if available schd_det then do:
                            zero_schedule_created = no.
                         end.
                         else do:
                            zero_schedule_created = yes.
                         end.
                    end. /* else do */
                end. /* if available sch_mstr */
                else do:
                     zero_schedule_created = no.
                end. /* else do */
             END PROCEDURE. /* procedure */
